package com.HuangJiaQing.www.service.impl;

import com.HuangJiaQing.www.dao.GoodsMapper;
import com.HuangJiaQing.www.dao.WareHouseMapper;
import com.HuangJiaQing.www.model.Goods;
import com.HuangJiaQing.www.model.GoodsExample;
import com.HuangJiaQing.www.model.WareHouse;
import com.HuangJiaQing.www.service.GoodsService;
import com.HuangJiaQing.www.service.WareHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author HONOR
 * @date 2022/7/29 9:55
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private WareHouseMapper wareHouseMapper;

    @Autowired
    private HttpSession session;

    public void insertGoods(Goods goods) {
        goodsMapper.insertSelective(goods);
    }

    public String fileUp(MultipartFile photo) {
        String fileName = photo.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        //将uuid和后缀名拼接后的结果作为最终的文件名
        fileName = uuid + suffixName;
        //通过ServletContext获取服务器中photo目录的路径
        ServletContext servletContext = session.getServletContext();
        String photoPath = servletContext.getRealPath("photo");
        File file = new File(photoPath);
        //判断photoPath所对应路径是否存在
        if (!file.exists()) {
            //若不存在，则创建目录
            file.mkdir();
        }
        String finalPath = photoPath + File.separator + fileName;
        //上传文件
        try {
            photo.transferTo(new File(finalPath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;
    }

    public List<Goods> selectAllGoods() {
        List<Goods> goods = goodsMapper.selectByExample(null);
        return goods;
    }

    public void deleteGoodsById(Integer id) {
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        WareHouse wareHouse = new WareHouse();
        wareHouse.setMarketerId(goods.getMarketersId());
        wareHouse.setAvatar(goods.getAvatar());
        wareHouse.setGoodsName(goods.getGoodsName());
        wareHouse.setGoodsPrice(goods.getGoodsPrice());
        wareHouse.setSum(goods.getNumber());
        wareHouseMapper.insertSelective(wareHouse);
        goodsMapper.deleteByPrimaryKey(id);
    }

    public List<Goods> searchGoodsAndMarketers(String search) {
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andGoodsNameLike("%" + search + "%");
        goodsExample.or().andMarketersNameLike("%" + search + "%");
        List<Goods> list = goodsMapper.selectByExample(goodsExample);
        return list;
    }

    public List<Goods> selectGoodsByMarketersName(String marketersName) {
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andMarketersNameEqualTo(marketersName);
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        return goods;
    }

    public List<Goods> selectGoodsByMarketersId(Integer id) {
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andMarketersIdEqualTo(id);
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        return goods;
    }

    public int forceDeleteGoods(Integer id) {
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        WareHouse wareHouse = new WareHouse();
        wareHouse.setMarketerId(goods.getMarketersId());
        wareHouse.setAvatar(goods.getAvatar());
        wareHouse.setGoodsName(goods.getGoodsName());
        wareHouse.setGoodsPrice(goods.getGoodsPrice());
        wareHouse.setIsForbidWarehouse(1);
        wareHouse.setSum(goods.getNumber());
        wareHouseMapper.insertSelective(wareHouse);
        goodsMapper.deleteByPrimaryKey(id);
        return goods.getMarketersId();
    }

    public Goods selectGoodsById(Integer id) {
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        return goods;
    }

    public void updateGoodsSelective(Goods goods) {
        goodsMapper.updateByPrimaryKeySelective(goods);
    }

}
